/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ocr.mpp;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.QueryOptions;
import com.datastax.driver.core.SocketOptions;
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
import com.datastax.driver.core.policies.DefaultRetryPolicy;
import java.util.List;

/**
 *
 * @author Austin
 */
public class CassandraCon {
    static org.apache.log4j.Logger logger = null;
    
    static {
        
        logger = org.apache.log4j.Logger.getLogger(CassandraCon.class.getName());
        
    }
        private Cluster clusterUI;
        private Session sessionUI;
        private PreparedStatement insert_statement = null;
        private List<ColumnMetadata> columns2;
    
        public void uiPStatement() {

        if (clusterUI != null || sessionUI != null || insert_statement != null) {
            return;
        }
        
        /*
        this.tableName = "tp_wxq_media_test";
            keyspace = "data_wxq";
            dataCenter = "data-wxq";
            coordinatorAddress = "10.144.16.18";
//        this.timestampKey = timestampKey;
        */

        for (int tryTimes = 0;; tryTimes++) {
            try {
                String dc = "data-wxq";
                String ks = "data_wxq";
                String tb = "tp_wxq_media_test";
                String ip = "10.144.16.18";
                String username = "cassandra";
                String passwd = "cassandra";
            
                int readTimeOut = 12000;
                Boolean retry = true;
                //�创建链接
                QueryOptions options = new QueryOptions();
                options.setConsistencyLevel(ConsistencyLevel.LOCAL_ONE);
                logger.info("11111111111111");
                Cluster.Builder builder = Cluster.builder();
                logger.info("222222222222");
                builder
                        .withSocketOptions(new SocketOptions().setConnectTimeoutMillis(120000).setReadTimeoutMillis(readTimeOut))
                        .withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withLocalDc(dc).build())
                        //.withRetryPolicy(DefaultRetryPolicy.INSTANCE)
                        .withCredentials(username, passwd).addContactPoint(ip).withQueryOptions(options);

                //设置重试策略�
                if (retry == null || retry) {
                    this.clusterUI = builder.withRetryPolicy(DefaultRetryPolicy.INSTANCE).build();
                } else {
                    this.clusterUI = builder.build();
                }
//绑定keyspace
                this.sessionUI = this.clusterUI.connect(ks);
                //获取该表元数据Ԫ���
                this.columns2 = this.clusterUI.getMetadata().getKeyspace(ks).getTable(tb).getColumns();
//                for (ColumnMetadata name : columns2) {
//                    columnMap2.put(name.getName(), resolveType(name.getType()));
//                }
                logger.info(columns2.toString());
                if (insert_statement == null) {
                    // 预编译insert语句
                    insert_statement = sessionUI.prepare("INSERT INTO " + tb + " JSON :json");
                    logger.info("INSERT INTO " + tb + " JSON :json");
                }
                break;
            } catch (Exception E) {
                logger.error(E.getMessage(), E);
            }
        }
    }
}
